/**
 * 营销顾问
 */
(function($){
  var goUrl = "http://120.24.49.23:6080/dpr/go/forward?uri=dzg/activity_recommend";
  /*
  自定义模板规则：推荐业务列表
  */
  Handlebars.registerHelper('businessList', function(items, options) {
    var out = '';
    var len = items.length;

    if(len > 0){
      //var count_num = 0;
      for(var i=0; i<len; i++) {
        //var item = options.fn(items[i]);
        var item = items[i];
        /*if(item.BUSINESSTYPE_LEVEL_1 != "MAINMODE"){
          continue;
        }*/
        var pageUrl = goUrl + "&" + getParams() + "activeName=" + encodeURIComponent(item.ACTIVENAME) + "&description=" + encodeURIComponent(item.DESCRIPTION) + "&mode=" +encodeURIComponent(item.BUSINESSTYPE_LEVEL_1) + "&fee_code=" + encodeURIComponent(item.BUSINESSTYPE_LEVEL_3) + "&index_url=" + encodeURIComponent(window.location.href);
        // console.log("pageUrl="+pageUrl);
        out = out + '<a href="' + pageUrl + '" class="'+options.hash.class+'">' + item.ACTIVENAME + '</a>';
        //count_num ++;
      }
     /* if(count_num < 1){
        out = '该用户暂无推荐的业务！';
      }*/
    }else{
      out = '该用户暂无推荐的业务！';
    }

    return out;
  });

  var showBaseInfo = function(data){
    // console.log(data);
    var basis = data.BASIS_LABEL;
    var market = data.MARKET_LABEL;
    //$("#user-name").html(data.USER_ID);
    console.log(basis);
    if(market){
      $("#user-star").html(market.CREDIT_CLASS ? market.CREDIT_CLASS : "");
      $("#prod-pric-name").html(market.PROD_PRIC_NAME ? market.PROD_PRIC_NAME : "");
      $("#is-broadband").html(market.KD_FLAG ? (market.KD_FLAG==1 ? "是" : "否") : "");
    }
    if(basis){
      $("#is-4G").html(basis.G4_USER_FLAG ? (basis.G4_USER_FLAG==1 ? "是" : "否") : "");//1是，0不是
      $("#term-model").html((basis.TERM_BRAND && basis.TERM_MODEL) ? basis.TERM_BRAND.trim()+basis.TERM_MODEL.trim() : "");
    }
  };
  var showDatas = function(data){
    // console.log(data);
    var listNew = new Array();
    if(data.ACTIVE_LIST){
      listNew = data.ACTIVE_LIST;
    }
    var $tpl = $('#business-tpl');
    var source = $tpl.text();
    var template = Handlebars.compile(source);
    var myData = {
      list: listNew
    };

    var html = template(myData);
    //$tpl.before(html);
    $("#business-list").html(html);
  }

  var loadDatas = function(){
    $('#modal').modal('open');
    var params = {"phone_no":$("#keyword").val()};
    // console.log(params);
    $.ajax({
      type: "post",
      url: "../marketing/search",
      data: params,
      dataType: "json",
      headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
        $('#modal').modal('close');
        // console.info(data);
        if(data.resCode == 0000000){
          $("#base-info").removeClass("am-hide");
          showBaseInfo(data.outData);
          //showDatas(data.outData);
        }else{
          $("#base-info").addClass("am-hide");
          var msg = data.resMsg;
          if(!msg){
            msg=data.message;
          }
          alert(msg);
        }
      },
      error : function(textStatus) {
        $('#modal').modal('close');
        $("#base-info").addClass("am-hide");
        alert("请求服务器失败，请联系管理员！");
        // console.info("request error");
      }
    });
  };

  $(function(){
    /*搜索按钮*/
    $("#search-icon,#search-btn").on("click",function(){
      var keyword = $("#keyword").val();
      if(!regularCheck(keyword,/^(13[4-9]|14[7]|15[012789]|18[23478]|209|17[8])\d{8}$/)){
        // showTip("请输入正确的手机号");
        layer.msg("请输入正确的手机号",{time:2000});
        //alert("请输入正确的手机号");
        $("#keyword").val("");
        return;
      }
      /*加载模态窗口*/
      var options = {closeViaDimmer: false};
      $('#modal').modal(options);

      loadDatas();
    });
  });

  //正则校验
function regularCheck(obj,patt){
  if(obj && patt){
    if(!patt.test(obj)){
      return false;
    }else{
      return true;
    }
  } else { return false; }
}
})(jQuery);
